package com.newbee.activity.tab;

import java.util.ArrayList;
import java.util.List;

import org.jivesoftware.smack.XMPPException;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.AlphaAnimation;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.hhu.newbee.R;
import com.newbee.activity.AddFriendsActivity;
import com.newbee.activity.MultChatChooseFriend;
import com.newbee.app.SpeechApp;
import com.newbee.fragement.FriendFragment;
import com.newbee.fragement.QunzuFragment;
import com.newbee.smack.SmackClient;

/**
 * 
 * 该类已经废弃了！
 * 
 * @author wch
 * 
 */

public class FriendsActivity extends FragmentActivity implements
		OnClickListener
{

	private ImageButton mAddFriends;
	private final int REQUEST_CODE = 1;

	private ViewPager mViewPager;

	private FragmentPagerAdapter mFragmentPagerAdapter;
	private List<Fragment> mFragments;

	private TextView mTagFriend;
	private TextView mTagQunzu;

	private int mScream_1_2;

	private ImageView mPoint;

	private int mCurrentIndex;

	private View mFriendsCountener;
	private View mQuZuCountener;

	private LinearLayout mAlertMenu;

	private boolean alertMenuIsShow;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{

		this.setContentView(R.layout.activity_friends);
		SpeechApp.add(this);
		initLayout();

		initViewPager();

		initPointer();
		super.onCreate(savedInstanceState);

	}

	private void initPointer()
	{
		Display display = getWindow().getWindowManager().getDefaultDisplay();

		DisplayMetrics displayMetrics = new DisplayMetrics();

		display.getMetrics(displayMetrics);
		mScream_1_2 = displayMetrics.widthPixels / 2;

		LayoutParams lp = mPoint.getLayoutParams();
		lp.width = mScream_1_2;
		mPoint.setLayoutParams(lp);

	}

	private void initViewPager()
	{
		this.mViewPager = (ViewPager) this.findViewById(R.id.viewpager_friends);
		this.mFragments = new ArrayList<Fragment>();
		this.mFragments.add(new FriendFragment());
		this.mFragments.add(new QunzuFragment());

		mFragmentPagerAdapter = new FragmentPagerAdapter(
				getSupportFragmentManager())
		{

			@Override
			public int getCount()
			{
				// TODO Auto-generated method stub
				return mFragments.size();
			}

			@Override
			public Fragment getItem(int arg0)
			{
				// TODO Auto-generated method stub
				return mFragments.get(arg0);
			}
		};
		mViewPager.setAdapter(mFragmentPagerAdapter);
		mViewPager.setOnPageChangeListener(new OnPageChangeListener()
		{

			@Override
			public void onPageSelected(int position)
			{

				resetTagTextColor();

				switch (position)
				{
				case 0:

					mTagFriend.setTextColor(getResources().getColor(
							R.color.steelblue));
					break;
				case 1:
					mTagQunzu.setTextColor(getResources().getColor(
							R.color.steelblue));
					break;
				}
				mCurrentIndex = position;

			}

			@Override
			public void onPageScrolled(int p, float positionOffset,
					int positionOffsetPx)
			{

				LinearLayout.LayoutParams lp = (android.widget.LinearLayout.LayoutParams) mPoint
						.getLayoutParams();

				if (mCurrentIndex == 0 && p == 0)// 0->1
				{
					lp.leftMargin = (int) (positionOffset * mScream_1_2 + mCurrentIndex
							* mScream_1_2);
				}
				else if (mCurrentIndex == 1 && p == 0)// 1->0
				{
					lp.leftMargin = (int) ((positionOffset - 1) * mScream_1_2 + mCurrentIndex
							* mScream_1_2);
				}
				mPoint.setLayoutParams(lp);
			}

			@Override
			public void onPageScrollStateChanged(int arg0)
			{
				// TODO Auto-generated method stub

			}
		});
	}

	protected void resetTagTextColor()
	{
		this.mTagFriend.setTextColor(getResources().getColor(R.color.black));
		this.mTagQunzu.setTextColor(getResources().getColor(R.color.black));

	}

	private void initLayout()
	{

		this.mAddFriends = (ImageButton) this.findViewById(R.id.btn_alertmenu);
		this.mAddFriends.setOnClickListener(this);
		this.mTagFriend = (TextView) this.findViewById(R.id.tag_friends);
		this.mTagQunzu = (TextView) this.findViewById(R.id.tag_qunzu);

		this.mPoint = (ImageView) this.findViewById(R.id.img_point);

		this.mFriendsCountener = this.findViewById(R.id.tag_friends_countener);
		this.mFriendsCountener.setOnClickListener(this);
		this.mQuZuCountener = this.findViewById(R.id.tag_qunzu_countener);

		this.mQuZuCountener.setOnClickListener(this);

		this.mAlertMenu = (LinearLayout) this.findViewById(R.id.alert_menu);
		this.mAlertMenu.setVisibility(View.GONE);
		this.findViewById(R.id.alert_menu_addfriend).setOnClickListener(this);
		this.findViewById(R.id.alert_menu_qunzu).setOnClickListener(this);

	}

	@Override
	public void onClick(View v)
	{
		switch (v.getId())
		{
		case R.id.btn_alertmenu:
		{
			toggleAlertMenu();
			break;
		}
		case R.id.tag_friends_countener:
		{

			mViewPager.setCurrentItem(0);
			break;

		}
		case R.id.tag_qunzu_countener:
		{
			mViewPager.setCurrentItem(1);
			break;
		}

		case R.id.alert_menu_addfriend:
		{

			Intent intent = new Intent(this, AddFriendsActivity.class);
			this.startActivityForResult(intent, REQUEST_CODE);
			toggleAlertMenu();
			break;
		}
		case R.id.alert_menu_qunzu:
		{

			Intent intent = new Intent(this, MultChatChooseFriend.class);
			this.startActivity(intent);
			toggleAlertMenu();
			break;

		}

		default:
			break;
		}

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data)
	{

		if (requestCode == REQUEST_CODE
				&& resultCode == AddFriendsActivity.RESPONSE_CODE)
		{

			Bundle bundle = data.getExtras();
			String account = bundle.getString(AddFriendsActivity.ACCOUNT);

			SmackClient sc = SmackClient.getInstance();
			try
			{
				sc.addFriend(account);
				Toast.makeText(this, "添加好友请求已发送……", Toast.LENGTH_LONG).show();
			}
			catch (XMPPException e)
			{
				Toast.makeText(this, "添加好友请求异常!", Toast.LENGTH_LONG).show();
				e.printStackTrace();
			}

		}

		super.onActivityResult(requestCode, resultCode, data);
	}

	public boolean onKeyDown(int keyCode, KeyEvent event)
	{

		if (keyCode == KeyEvent.KEYCODE_BACK)
		{

			SpeechApp.exit();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	private void toggleAlertMenu()
	{
		if (alertMenuIsShow)
		{
			alertMenuIsShow = false;
			// 此时关闭alertmenu
			closeAlertMenu();
		}
		else
		{
			alertMenuIsShow = true;
			// 此时打开alertmenu
			openAlertMenu();
		}
	}

	private void openAlertMenu()
	{
		mAlertMenu.setVisibility(View.VISIBLE);

	}

	private void closeAlertMenu()
	{

		mAlertMenu.setVisibility(View.GONE);
	}
}
